import GameManager from "./GameManager";

export default class MyPLayerContorller extends Laya.Script {

    constructor() { 
        super(); 
        /** @prop {name:shoe,tips:"旋转鞋子",type:Node,default:null}*/
        /** @prop {name:zuo,tips:"旋转鞋子",type:Node,default:null}*/
        /** @prop {name:you,tips:"旋转鞋子",type:Node,default:null}*/
        /** @prop {name:shang,tips:"旋转鞋子",type:Node,default:null}*/
        this.shoe=null;
        this.zuo=null;
        this.shang=null;
        
        this.shoe=null;
        this.rig=null;
        this.catJump=true;
        this.gameManager=true;

        this.canLeft=false;
        this.canRight=false;

        // 更多参数说明请访问: https://ldc2.layabox.com/doc/?nav=zh-as-2-4-0
    }
    reset(){
        this.gameManager.AddAIScore();
        this.owner.x=443
        this.owner.y=850
        this.rig.setVelocity({x:0,y:0})
    }
    
    onAwake(){
        this.gameManager=this.owner.parent.getComponent(GameManager);
        this.rig=this.owner.getComponent(Laya.RigidBody)
        Laya.stage.on("ResetMyPlayer",this,this.reset)

        this.zuo.on(Laya.Event.MOUSE_DOWN,this,function(){
            this.canLeft=true;
        })
        this.zuo.on(Laya.Event.MOUSE_UP,this,function(){
            this.canLeft=false;
        })
        this.zuo.on(Laya.Event.MOUSE_OUT,this,function(){
            this.canLeft=false;
        })
        this.you.on(Laya.Event.MOUSE_DOWN,this,function(){
            this.canRight=true;
        })
        this.you.on(Laya.Event.MOUSE_UP,this,function(){
            this.canRight=false;
        })
        this.you.on(Laya.Event.MOUSE_OUT,this,function(){
            this.canRight=false;
        })
        this.shang.on(Laya.Event.CLICK,this,this.Jump)

    }
    onDestroy(){
        Laya.stage.on("ResetMyPlayer",this,this.reset)
    }
    MoveLeft(){
        if(this.gameManager.gameOver)return;
            // var y=this.rig.linearVelocity.y
            // this.rig.setVelocity({x:-15,y:y})
            var x=this.rig.x;
            this.rig.x=x+10;
    }
    MoveRight(){
        if(this.gameManager.gameOver)return;
            var y=this.rig.linearVelocity.y
            this.rig.setVelocity({x:15,y:y})
    }
    Jump(){
        if(this.catJump==false)return;
        if(this.gameManager.gameOver)return;
            this.catJump=false;
            this.rig.setVelocity({x:0,y:-11})

    }
    onUpdate(){
        if(this.canLeft){
            this.MoveLeft();
        }
        if(this.canRight){
           this.MoveRight(); 
        }
        this.rotationShoe();
        if(Laya.KeyBoardManager.hasKeyDown(Laya.Keyboard.A)){
            this.MoveLeft();
        }
        if(Laya.KeyBoardManager.hasKeyDown(Laya.Keyboard.D)){
            this.MoveRight();
        }
        if(this.owner.y>840){
            this.catJump=true;
        }
    }
    onKeyDown(e){
        if(e.nativeEvent.key==" "){
            this.Jump();
            
        }
    }
    rotationShoe(){
        if(this.rig.linearVelocity.x>0.1){
            this.shoe.rotation=90;
        }
        else if(this.rig.linearVelocity.x<-0.1){
            this.shoe.rotation=-90;
        }
        else{
            this.shoe.rotation=0;
        }

    }
    
}